<template>
  <el-dialog model-value="dialogVisible" :title="dialogTitle" width="30%" @close="handleClose">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="上级菜单" prop="parentId">
        <el-tree-select v-model="form.parentId" :data="tableData" :render-after-expand="false" :check-strictly="true"
          style="width: 240px">
          <!-- 其他选项 -->
          <template #header>
            <el-option :key="0" :label="'一级菜单'" :value="0" />
          </template>
          <!-- 遍历子数据 -->
          <template #default="{ data: { name, id } }">
            <el-option :key="id" :label="name" :value="id" />
          </template>
        </el-tree-select>
      </el-form-item>

      <el-form-item label="菜单类型" prop="menuType" label-width="100px">
        <el-radio-group v-model="form.menuType">
          <el-radio :label="'M'">目录</el-radio>
          <el-radio :label="'C'">菜单</el-radio>
          <el-radio :label="'F'">按钮</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="菜单图标" prop="icon">

        <el-select v-model="form.icon" placeholder="选择图标" size="large" style="width: 240px">
          <el-option v-for="item in svgList" :key="item" :label="item" :value="item">
            <el-icon><svg-icon :icon="item" /></el-icon>
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="权限标识" prop="perms">
        <el-input v-model="form.perms" />
      </el-form-item>

      <el-form-item label="组件路径" prop="component">
        <el-input v-model="form.component" />
      </el-form-item>

      <el-form-item label="显示顺序" prop="orderNum">
        <el-input-number v-model="form.orderNum" :min="1" label="显示顺序"></el-input-number>
      </el-form-item>



    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>

import { defineEmits, defineProps, ref, watch } from "vue";
import requestUtil, { getServerUrl } from "@/util/request";
import { ElMessage } from 'element-plus'

const tableData = ref([])

// 父组件传递的数据
const props = defineProps(
  {
    id: {
      type: Number,
      default: -1,
      required: true
    },
    dialogTitle: {
      type: String,
      default: '',
      required: true
    },
    dialogVisible: {
      type: Boolean,
      default: false,
      required: true
    },
    tableData: {
      type: Array,
      default: [],
      required: true
    }
  }
)


const form = ref({
  id: -1,
  parentId: 1,
  menuType: "M",
  icon: '',
  name: '',
  perms: '',
  component: '',
  orderNum: 1
})

// 表单规则
const rules = ref({
  parentId: [
    { required: true, message: '请选择上级菜单' }
  ],
  name: [{ required: true, message: "菜单名称不能为空", trigger: "blur" }]
})

const formRef = ref(null)
// 初始化表单
const svgList = ref([]);
// 初始化标签
const initSvgData = async () => {
  console.log("初始化数据");
  const svg = await requestUtil.get("crmMenu/iconList");
  svgList.value = svg.data.data
}

const initFormData = async (id) => {
  const res = await requestUtil.get("crmMenu/" + id);
  form.value = res.data.data;
}


//观察着模式
watch(
  () => props.dialogVisible,
  () => {
    let id = props.id;
    tableData.value = props.tableData;
    console.log("watch,di", id, props.dialogVisible);
    initSvgData();
    if (id != -1 && props.dialogVisible) {

      initFormData(id)
    } else {
      form.value = {
        id: -1,
        parentId: 1,
        menuType: "M",
        icon: '',
        name: '',
        perms: '',
        component: '',
        orderNum: 1
      }
    }
  }
)


const emits = defineEmits(['update:modelValue', 'initSvgData'])

const handleClose = () => {
  emits('update:modelValue', false)
}

// 条件按钮
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let result = await requestUtil.post("crmMenu/save", form.value);
      let data = result.data;
      if (data.code == 1) {
        ElMessage.success("执行成功！")
        formRef.value.resetFields();
        emits("initSvgData", "")
        handleClose();
      } else {
        ElMessage.error(data.message);
      }
    } else {
      ElMessage.error("添加失败");
    }
  })
}

</script>

<style scoped></style>
